package cn.wolfcode.Controller;


import cn.wolfcode.domain.Salary;
import cn.wolfcode.qo.SalaryObject;
import cn.wolfcode.service.EmployeeService;
import cn.wolfcode.service.ISalaryService;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/salary")
public class SalaryController {

    @Autowired
    private ISalaryService salaryService;

    @Autowired
    private EmployeeService employeeService;


    @RequirePermission(name = "工资单页面",experssion = "salary:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SalaryObject qo){
        PageInfo<Salary> pageInfo = salaryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("employees", employeeService.selectAll());

        return "salary/list";
    }

    @RequestMapping("/delete")
@RequirePermission(name = "工资单页面",experssion = "salary:list")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            salaryService.delete(id);
        }
        return new jsonResult();
    }


    @RequestMapping("/saveOrUpdate")
@RequirePermission(name = "工资单页面",experssion = "salary:list")
    @ResponseBody
    public jsonResult saveOrUpdate(Salary salary){
        if (salary.getId() != null) {
            salaryService.update(salary);
        }else {
            salaryService.save(salary);
        }
        return new jsonResult();
    }
}
